/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.gui;

import aptech.semester.code.common.Role;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import aptech.semester.code.custom.component.BackgroundImageDesktopPane;
import aptech.semester.code.entity.EmployeeEntity;


/**
 *
 * @author vi
 */
public class MainScreen extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    public MainScreen() {
        initComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        setLocationRelativeTo(this);
        preLogin();
        mnItemConnection.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopPane = new BackgroundImageDesktopPane();
        jToolBar1 = new javax.swing.JToolBar();
        jLabel1 = new javax.swing.JLabel();
        lblNameLogin = new javax.swing.JLabel();
        filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel2 = new javax.swing.JLabel();
        lblPosistion = new javax.swing.JLabel();
        filler2 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(32767, 0));
        jLabel3 = new javax.swing.JLabel();
        lblDepartment = new javax.swing.JLabel();
        mainMenu = new javax.swing.JMenuBar();
        mnSystem = new javax.swing.JMenu();
        mnItemLogin = new javax.swing.JMenuItem();
        mnItemLogout = new javax.swing.JMenuItem();
        mnItemConnection = new javax.swing.JMenuItem();
        mnItemExit = new javax.swing.JMenuItem();
        mnHumanResource = new javax.swing.JMenu();
        mnItemEmployeeManagement = new javax.swing.JMenuItem();
        mnItemSalary = new javax.swing.JMenuItem();
        mnGeneralInfo = new javax.swing.JMenu();
        mnItemPosition = new javax.swing.JMenuItem();
        mnItemDepartment = new javax.swing.JMenuItem();
        mnItemProvince = new javax.swing.JMenuItem();
        mnItemEducation = new javax.swing.JMenuItem();
        mnItemSpecialize = new javax.swing.JMenuItem();
        mnItemBank = new javax.swing.JMenuItem();
        mnReport = new javax.swing.JMenu();
        mnItemReportEmployee = new javax.swing.JMenuItem();
        mnAccount = new javax.swing.JMenu();
        mnItemManageAccount = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Human Resource Management System");
        setResizable(false);

        javax.swing.GroupLayout desktopPaneLayout = new javax.swing.GroupLayout(desktopPane);
        desktopPane.setLayout(desktopPaneLayout);
        desktopPaneLayout.setHorizontalGroup(
            desktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 656, Short.MAX_VALUE)
        );
        desktopPaneLayout.setVerticalGroup(
            desktopPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 427, Short.MAX_VALUE)
        );

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setFocusable(false);

        jLabel1.setText("Name : ");
        jToolBar1.add(jLabel1);
        jToolBar1.add(lblNameLogin);
        jToolBar1.add(filler1);

        jLabel2.setText("Posistion : ");
        jToolBar1.add(jLabel2);
        jToolBar1.add(lblPosistion);
        jToolBar1.add(filler2);

        jLabel3.setText("Department : ");
        jToolBar1.add(jLabel3);
        jToolBar1.add(lblDepartment);

        mnSystem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/system.png"))); // NOI18N
        mnSystem.setText("System");

        mnItemLogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/login.png"))); // NOI18N
        mnItemLogin.setText("Login");
        mnItemLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemLoginActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemLogin);

        mnItemLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/logout.png"))); // NOI18N
        mnItemLogout.setText("Logout");
        mnItemLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemLogoutActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemLogout);

        mnItemConnection.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/conn.png"))); // NOI18N
        mnItemConnection.setText("Connect Database");
        mnItemConnection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemConnectionActionPerformed(evt);
            }
        });
        mnSystem.add(mnItemConnection);

        mnItemExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/exit.png"))); // NOI18N
        mnItemExit.setText("Exit");
        mnSystem.add(mnItemExit);

        mainMenu.add(mnSystem);

        mnHumanResource.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/hr.png"))); // NOI18N
        mnHumanResource.setText("Human Resource");

        mnItemEmployeeManagement.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/emp_management.png"))); // NOI18N
        mnItemEmployeeManagement.setText("Employee Management");
        mnItemEmployeeManagement.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemEmployeeManagementActionPerformed(evt);
            }
        });
        mnHumanResource.add(mnItemEmployeeManagement);

        mnItemSalary.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/salary.png"))); // NOI18N
        mnItemSalary.setText("Payroll");
        mnItemSalary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemSalaryActionPerformed(evt);
            }
        });
        mnHumanResource.add(mnItemSalary);

        mainMenu.add(mnHumanResource);

        mnGeneralInfo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/general_info.png"))); // NOI18N
        mnGeneralInfo.setText("Resource Info");

        mnItemPosition.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/position.png"))); // NOI18N
        mnItemPosition.setText("Position");
        mnItemPosition.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemPositionActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemPosition);

        mnItemDepartment.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/department.png"))); // NOI18N
        mnItemDepartment.setText("Department");
        mnItemDepartment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemDepartmentActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemDepartment);

        mnItemProvince.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/province.png"))); // NOI18N
        mnItemProvince.setText("Province");
        mnItemProvince.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemProvinceActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemProvince);

        mnItemEducation.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/education.png"))); // NOI18N
        mnItemEducation.setText("Education Level");
        mnItemEducation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemEducationActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemEducation);

        mnItemSpecialize.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/specialized.png"))); // NOI18N
        mnItemSpecialize.setText("Specialized");
        mnItemSpecialize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemSpecializeActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemSpecialize);

        mnItemBank.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/bank.png"))); // NOI18N
        mnItemBank.setText("Bank");
        mnItemBank.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemBankActionPerformed(evt);
            }
        });
        mnGeneralInfo.add(mnItemBank);

        mainMenu.add(mnGeneralInfo);

        mnReport.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report.png"))); // NOI18N
        mnReport.setText("Report");

        mnItemReportEmployee.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/report_emp.png"))); // NOI18N
        mnItemReportEmployee.setText("Report Employee");
        mnItemReportEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemReportEmployeeActionPerformed(evt);
            }
        });
        mnReport.add(mnItemReportEmployee);

        mainMenu.add(mnReport);

        mnAccount.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/account.png"))); // NOI18N
        mnAccount.setText("Account");

        mnItemManageAccount.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/mn_acc.png"))); // NOI18N
        mnItemManageAccount.setText("Manage Account");
        mnItemManageAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnItemManageAccountActionPerformed(evt);
            }
        });
        mnAccount.add(mnItemManageAccount);

        mainMenu.add(mnAccount);

        setJMenuBar(mainMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopPane)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(desktopPane)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnItemLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemLoginActionPerformed
        addNewInternalFrame(new Login(this));
    }//GEN-LAST:event_mnItemLoginActionPerformed

    private void mnItemConnectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemConnectionActionPerformed
        
        addNewInternalFrame(new ConnectDatabase(this));
    }//GEN-LAST:event_mnItemConnectionActionPerformed

    private void mnItemLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemLogoutActionPerformed
        loginEmployee = null;
        role = -1;
        preLogin();
        addNewInternalFrame(new Login(this));
        fillInfoEmployeeLogin("", "", "");
        closeAllWindow();
    }//GEN-LAST:event_mnItemLogoutActionPerformed

    private void mnItemEducationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemEducationActionPerformed
        addNewInternalFrame(new Education(this));
    }//GEN-LAST:event_mnItemEducationActionPerformed

    private void mnItemReportEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemReportEmployeeActionPerformed
        addNewInternalFrame(new EmployeeReport(this));
    }//GEN-LAST:event_mnItemReportEmployeeActionPerformed

    private void mnItemEmployeeManagementActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemEmployeeManagementActionPerformed
        addNewInternalFrame(new EmployeeManagement(this));
    }//GEN-LAST:event_mnItemEmployeeManagementActionPerformed

    private void mnItemPositionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemPositionActionPerformed
        addNewInternalFrame(new Position(this));
    }//GEN-LAST:event_mnItemPositionActionPerformed

    private void mnItemDepartmentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemDepartmentActionPerformed
        addNewInternalFrame(new Department(this));
    }//GEN-LAST:event_mnItemDepartmentActionPerformed

    private void mnItemProvinceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemProvinceActionPerformed
        addNewInternalFrame(new Province(this));
    }//GEN-LAST:event_mnItemProvinceActionPerformed

    private void mnItemSpecializeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemSpecializeActionPerformed
        addNewInternalFrame(new Specialized(this));
    }//GEN-LAST:event_mnItemSpecializeActionPerformed

    private void mnItemBankActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemBankActionPerformed
        addNewInternalFrame(new Bank(this));
    }//GEN-LAST:event_mnItemBankActionPerformed

    private void mnItemSalaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemSalaryActionPerformed
        addNewInternalFrame(new EmployeeSalary(this));
    }//GEN-LAST:event_mnItemSalaryActionPerformed

    private void mnItemManageAccountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnItemManageAccountActionPerformed
        addNewInternalFrame(new ManageAccount(this));
    }//GEN-LAST:event_mnItemManageAccountActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainScreen().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopPane;
    private javax.swing.Box.Filler filler1;
    private javax.swing.Box.Filler filler2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblDepartment;
    private javax.swing.JLabel lblNameLogin;
    private javax.swing.JLabel lblPosistion;
    private javax.swing.JMenuBar mainMenu;
    private javax.swing.JMenu mnAccount;
    private javax.swing.JMenu mnGeneralInfo;
    private javax.swing.JMenu mnHumanResource;
    private javax.swing.JMenuItem mnItemBank;
    private javax.swing.JMenuItem mnItemConnection;
    private javax.swing.JMenuItem mnItemDepartment;
    private javax.swing.JMenuItem mnItemEducation;
    private javax.swing.JMenuItem mnItemEmployeeManagement;
    private javax.swing.JMenuItem mnItemExit;
    private javax.swing.JMenuItem mnItemLogin;
    private javax.swing.JMenuItem mnItemLogout;
    private javax.swing.JMenuItem mnItemManageAccount;
    private javax.swing.JMenuItem mnItemPosition;
    private javax.swing.JMenuItem mnItemProvince;
    private javax.swing.JMenuItem mnItemReportEmployee;
    private javax.swing.JMenuItem mnItemSalary;
    private javax.swing.JMenuItem mnItemSpecialize;
    private javax.swing.JMenu mnReport;
    private javax.swing.JMenu mnSystem;
    // End of variables declaration//GEN-END:variables
    private EmployeeEntity loginEmployee;
    private int role;
// private
    public void addNewInternalFrame(JInternalFrame frame) {
        if (checkWindowAvailable(frame)) {
            desktopPane.add(frame);
            frame.setVisible(true);
        } else {
            return;
        }
    }
    
    //check window has been available ?
    private boolean checkWindowAvailable(JInternalFrame window) {
        for (JInternalFrame inFrame : desktopPane.getAllFrames()) {
	if (inFrame.getTitle() == window.getTitle()) {
                return false;
            }
        }
            return true;
    }
    
    // close all window
    private void closeAllWindow() {
        for (JInternalFrame inFrame : desktopPane.getAllFrames()) {
            inFrame.dispose();
        }
    }
    
    public void fillInfoEmployeeLogin(String name, String departnament, String position) {
        lblNameLogin.setText(name);
        lblDepartment.setText(departnament);
        lblPosistion.setText(position);
    }

    private void preLogin() {
        mnHumanResource.setVisible(false);
        mnItemEmployeeManagement.setVisible(false);
        mnItemSalary.setVisible(false);
        
        mnGeneralInfo.setVisible(false);
        mnItemPosition.setVisible(false);
        mnItemDepartment.setVisible(false);
        mnItemProvince.setVisible(false);
        mnItemSpecialize.setVisible(false);
        mnItemBank.setVisible(false);
        
        mnReport.setVisible(false);
        mnItemReportEmployee.setVisible(false);
        mnItemLogout.setEnabled(false);
        
        // account
        mnAccount.setVisible(false);
        mnItemManageAccount.setVisible(false);
    }
    
    public void afterLogin() {
        mnItemLogout.setEnabled(true);
        if (role == Role.ROLE_ADMIN) {
            // human resource
            mnHumanResource.setVisible(true);
            mnItemEmployeeManagement.setVisible(true);
            mnItemSalary.setVisible(true);
            // general info    
            mnGeneralInfo.setVisible(true);
            mnItemPosition.setVisible(true);
            mnItemDepartment.setVisible(true);
            mnItemProvince.setVisible(true);
            mnItemSpecialize.setVisible(true);
            mnItemBank.setVisible(true);
            // report
            mnReport.setVisible(true);
            mnItemReportEmployee.setVisible(true);
            // account
            mnAccount.setVisible(true);
            mnItemManageAccount.setVisible(true);
            
        } else if (role == Role.ROLE_HR) {
            // human resource
            mnHumanResource.setVisible(true);
            mnItemEmployeeManagement.setVisible(true);
            
            mnItemSalary.setVisible(true);
            
            mnReport.setVisible(true);
            mnItemReportEmployee.setVisible(true);
        } else if (role == Role.ROLE_MANAGER) {
            // human resource
            mnHumanResource.setVisible(true);
            mnItemEmployeeManagement.setVisible(true);
        }
    }
    public EmployeeEntity getLoginEmployee() {
        return loginEmployee;
    }

    public void setLoginEmployee(EmployeeEntity loginEmployee) {
        this.loginEmployee = loginEmployee;
    }

    public int getRole() {
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }
    
    
}
